$(function() {
	var teaGradua = $("#teaGradua");
	teaGradua.datagrid({
				url : 'teaemploy/graduations',
				fit : true,
				rownumbers : true,
				pagination : true,
				fitColumns : true,
				columns : [ [
						{
							field : 'checkbox',
							width : 100,
							align : 'center',
							checkbox : true
						},
						{
							field : 'tb_student_id',
							width : 20,
							align : 'center',
							hidden : true
						},
						{
							field : 'student_name',
							title : '姓名',
							align : 'center',
							width : 100
						},
						{
							field : 'gender',
							title : '性别',
							align : 'center',
							width : 80,
							formatter : function(value, rec) {
								if (rec.gender == '男') {
									return '<img src="public/images/male.png" title="我是帅锅">'
								} else {
									return '<img src="public/images/female.png" title="我是妹子">'
								}
							}
						},
						{
							field : 'education',
							title : '学员类型',
							align : 'center',
							width : 100
						},
						{
							field : 'class_name',
							title : '班级',
							align : 'center',
							width : 100
						},
						{
							field : 'class_type',
							title : '班级类型',
							align : 'center',
							width : 100
						},
						{
							field : 'count',
							title : '作业次数',
							align : 'center',
							width : 80
						},
						{
							field : 'wc',
							title : '操作',
							width : 100,
							align : 'center',
							formatter : function(value, row, index) {
								var bts = [];
								bts.push('<a data-id="' + row.tb_student_id
										+ '" data-index="' + index
										+ '" class="ctr delete">打回</a>');
								return bts.join('');
							}
						} ] ],
				toolbar : [ {
					iconCls : 'icon-add',
					text : '批量添加学员',
					handler : function() {
						batch();
					}
				} ]
			});

	var name = $("#graname");
	var classes = $("#graclass");
	var stutype = $("#stutype");
	var classtype = $("#classtype");
	$("#graSearch").on('click', function() {
		teaGradua.datagrid("load", {
			name : "%" + name.val() + "%",
			className : "%" + classes.val() + "%",
			stuType : stutype.val(),
			classType : classtype.val()
		});
	});

	var form = $("#graSearchForm");
	$("#graReset").on('click', function() {
		form.form('clear');
		teaGradua.datagrid("load", {});
	});

	var graPanel = teaGradua.datagrid("getPanel");
	graPanel.on("click", "a.delete", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要打回此学员吗？", function(r) {
			if (r) {
				$.get("teaemploy/delete/" + id, function(rs) {
					if (rs.success) {
						teaGradua.datagrid("reload");
					} else {
						$.messager.alert("提示", rs.message);
					}
				});
			}
		});
	});

	function batch() {
		$.messager.confirm("提示", "是否确定批量操作？", function(r) {
			if (r) {
				var checkeds = teaGradua.datagrid('getChecked');
				var idStr = '';
				for (var i = 0; i < checkeds.length; i++) {
					idStr += checkeds[i].tb_student_id + ",";
				}
				if (idStr.length == 0) {
					$.messager.alert('提示', '请选择要操作的学生');
					return;
				}
				var ids = new Array();
				for (var j = 0; j < checkeds.length; j++) {
					ids[j] = checkeds[j].tb_student_id;
				}
				var tag = 0;
				$.post("teaemploy/batch/" + ids, function(rs) {
					if (rs.success) {
						// 刷新数据表格
						$("#teaRecent").datagrid("reload");
						teaGradua.datagrid("reload");
					} else {
						$.messager.alert("提示", rs.message);

					}
				});
			}
		});
	}

});